using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using BL;
using Telerik.WinControls.UI;
using BO;

namespace AnimalFarm.Product_manage
{
    public partial class AddMix : Telerik.WinControls.UI.RadForm
    {
        B_Livestock B_lives = new B_Livestock();
        B_Building B_building = new B_Building();
        B_Mix B_mix = new B_Mix();
        B_LiveStock_Mix B_livestockMix = new B_LiveStock_Mix();
        public AddMix()
        {
            InitializeComponent();
        }

        //load livestock for milking
        private void loadLivestock()
        {
            DataTable aTable = B_lives.GetAllLivestockByLivestockType(1);
            grvLivestockList.DataSource = aTable;
        }

        //load building for milking
        private void loadBuilding()
        {
            DataTable aTable = B_building.GetAllBuilding();
            drdBuilding.DataSource = aTable;
            drdBuilding.DisplayMember = "BuildingName";
            drdBuilding.ValueMember = "BuildingID";
            drdBuilding.Text = "(Select a location)";
        }

        private void AddMix_Load(object sender, EventArgs e)
        {
            loadLivestock();
            loadBuilding();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btnAddMix_Click(object sender, EventArgs e)
        {
            int []livestockIDs=new int[grvLivestockList.SelectedRows.Count];
            if (grvLivestockList.SelectedRows.Count > 0)
            {
                //set a array of livestock ID
                for(int i=0;i<grvLivestockList.SelectedRows.Count;i++)
                {
                    GridViewRowInfo row = grvLivestockList.SelectedRows[i];
                    livestockIDs[i] = int.Parse(row.Cells["LivestockID"].Value.ToString());
                }

                //getting data for inserting to the mix table
                string mixName = txtMixName.Text;
                DateTime time = dtpTimeMix.Value;
                int buildingID = int.Parse(drdBuilding.SelectedValue.ToString());
                string description = txtDescription.Text;
                int quanlity = 0;
                try
                {
                    quanlity = int.Parse(txtQuanlity.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("The quanlity must a number");
                }

                Mix_Info mix = new Mix_Info();
                mix.MixLocationID = buildingID;
                mix.MixName = mixName;
                mix.Quanlity = quanlity;
                mix.Time = time;
                mix.Description = description;
                bool isOk = B_mix.CreateOneMix(mix);
                if (isOk)
                {
                    DataTable aTable = B_mix.GetAllMix();
                    int mixID=0,aMixID;
                    //get current mix inserted before
                    for (int i = 0; i < aTable.Rows.Count; i++)
                    {
                        aMixID = int.Parse(aTable.Rows[i]["MixID"].ToString());
                        if (mixID < aMixID)
                            mixID = aMixID;
                    }
                    //insert data for the LiveStock_Mix table
                    bool insert = true;
                    for (int i = 0; i < livestockIDs.Length; i++)
                    {
                        LiveStock_Mix_Info livestockMix = new LiveStock_Mix_Info();
                        livestockMix.LivestockID = livestockIDs[i];
                        livestockMix.MixID = mixID;

                        insert = B_livestockMix.CreateOneLiveStock_Mix(livestockMix);
                    }
                    if (insert)
                    {
                        MessageBox.Show(this, "Milking mix successful", "Milking mix", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        resetFields();
                    }
                }
            }
            else
            {
                MessageBox.Show(this, "You must least one livestock", "Milking mix", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void resetFields()
        {
            txtDescription.Text = "";
            txtMixName.Text = "";
            txtQuanlity.Text = "";
            txtMixName.Focus();
        }

        private void btnAddMilkTreatment_Click(object sender, EventArgs e)
        {
            new AddMilkTreatment().Show();
        }
    }
}
